# coding: utf-8
from typing import List

from parso import parse


class Solution:
    # 只有加法和减法和括号
    # 使用两个栈
    def calculate(self, s: str) -> int:
        op = []
        nums = []
        i = 0
        while i < len(s):
            ch = s[i]
            if ch == '(':
                continue
            elif ch == ')':
                sum = nums[-1]+nums[-2]
                nums.pop()
                nums.pop()
                nums.append(sum)
                i = i + 1
            elif ch == '-':
                num, i = self.changeInt(s, i+1)
                nums.append(-1 * num)
                op.append('+')
            elif ch == '+':
                num, i = self.changeInt(s, i+1)
                nums.append(num)
                op.append('-')
            elif ch == ' ' or (ch >= '0' and ch <= '9'):
                num, i = self.changeInt(s, i)
                nums.append(num)

        num = 0

    def changeInt(self, s: str, begin: int) -> int:
        ans = 0
        i = begin
        while i < len(s) and s[i] != '+' and s[i] != '-' and s[i] != '(' and s[i] != ')':
            if(s[i] >= '0' and s[i] <= '9'):
                ans = ans*10 + int(s[i])
            i = i+1
        return ans, i


if __name__ == '__main__':
    solu = Solution()
    print(solu.calculate('1+ 2 -1-2'))
